package com.bora_bandeco;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class ContatosListAdapter extends BaseAdapter {
	private Context mContext;
	
	private List<Contato> contatos;
	private List<Boolean> checked;

	
	public ContatosListAdapter(Context c) {
		this.mContext = c;
		this.contatos = new ArrayList<Contato>();
		this.checked = new ArrayList<Boolean>();
	}
	
	public void addItem(Contato c) {
		this.contatos.add(c);
		this.checked.add(false);
	}

	@Override
	public int getCount() {
		return this.contatos.size();
	}


	@Override
	public Object getItem(int position) {
		return this.contatos.get(position);
	}

	public List<Contato> getContatos() {
		return this.contatos;
	}

	public boolean isChecked(int position) {
		return this.checked.get(position);
	}
	
	@Override
	public long getItemId(int position) {
		return this.contatos.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Contato contato = this.contatos.get(position);
		
		LayoutInflater li =  (LayoutInflater)this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		View tempView = li.inflate(R.layout.item_contato, null);
		
		ImageView foto = (ImageView) tempView.findViewById(R.id.foto);
		
		Bitmap bitmap = null;
		
		// Se for o meu usuário, buscar dados nas preferências
		SharedPreferences preferences = mContext.getSharedPreferences("com.bora_bandeco", Context.MODE_PRIVATE);
        int meuIdContato = preferences.getInt("_id", 0);
        
        if (meuIdContato == contato.getId()) {
        	String minhaFotoCodificada = preferences.getString("foto", "");
        	
        	if (!minhaFotoCodificada.equalsIgnoreCase("")) {
        		byte[] decodedString = Base64.decode(minhaFotoCodificada, Base64.DEFAULT);
        		bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        	}
        } else {
        	bitmap = contato.getBitmap();
        }
		
		if (bitmap != null) {
			foto.setImageBitmap(bitmap);
		}
        
        TextView nome = (TextView) tempView.findViewById(R.id.nome);
		nome.setText(contato.getNome());
		
        
		tempView.setTag(position);
		
		// Marca o checkbox ao clicar em algum local qualquer da view
		tempView.setOnClickListener(new OnClickListener() {
	        @Override
			public void onClick(View view) {
	        	int bottom = view.getPaddingBottom();
        	    int top = view.getPaddingTop();
        	    int right = view.getPaddingRight();
        	    int left = view.getPaddingLeft();
	        	CheckBox checkbox = (CheckBox) view.findViewById(R.id.checkbox);
	        	Integer position = (Integer)view.getTag();
	        	
	        	if (checkbox.isChecked()) {
	        		checkbox.setChecked(false);
	        		view.setBackgroundDrawable(ContatosListAdapter.this.mContext.getResources().getDrawable(R.drawable.item_convidado));
	        		view.setPadding(left, top, right, bottom);
	        		
	        		
	        		ContatosListAdapter.this.checked.set(position.intValue(), false);
	        	} else {
	        		checkbox.setChecked(true);
	        		view.setBackgroundDrawable(ContatosListAdapter.this.mContext.getResources().getDrawable(R.drawable.item_convidado_background_pressed));
	        		view.setPadding(left, top, right, bottom);
	        		
	        		ContatosListAdapter.this.checked.set(position.intValue(), true);
	        	}
	        }
	    });

		return tempView;
	}
}